package com.schooleducation.controller.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonOneType;
import com.schooleducation.entity.LessonTwoType;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/teacher/updateLesson")
public class UpdateLessonController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	private List<LessonOneType> lessonOneTypes;

	@RequestMapping(value = "{id:\\+?[1-9][0-9]*$}", method = RequestMethod.GET)
	public String getPage(@PathVariable(value = "id") int lessonId, HttpSession session, Model model) {
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			Lesson lesson = serviceFac.getLessonService().getLessonById(lessonId);
			System.out.println(lesson.getLessonType().getId());
			lessonOneTypes = serviceFac.getLessonService().getLessonTypeAll();

			for (LessonOneType lessonOneType : lessonOneTypes) {
				for (LessonTwoType lessonTwoType : lessonOneType.getLessonTwoType()) {
					if (lessonTwoType.getId() == lesson.getLessonType().getId()) {
						model.addAttribute("oneType", lessonOneType);
						model.addAttribute("lessonTwoType", lessonOneType.getLessonTwoType());
					}
				}
			}
			model.addAttribute("lessonOneType", lessonOneTypes);
			model.addAttribute("lesson", lesson);
			model.addAttribute("lessonInfo", serviceFac.getLessonService().getLessonInfoById(lessonId));
			model.addAttribute("teacher", teacher);

			return "teacher/updateLesson";
		} else {
			return "redirect:../../login";
		}
	}

	/**
	 * 动态获取获取二级课程类型
	 * 
	 * @param index
	 * @return
	 */
	@RequestMapping(value = "lessonType", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getLessonTwoType(int index) {

		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "oneType" });

		return JSONArray.fromObject(lessonOneTypes.get(index).getLessonTwoType(), config).toString();
	}

	/**
	 * 课程信息更新提交
	 * 
	 * @param session
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "updateLesson", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateLesson(HttpSession session, String data) {
		Map<String, Object> state = new HashMap<String, Object>();
		User teacher = (User) session.getAttribute("teacher");
		if (teacher == null) {
			state.put("isLogin", "no");
		} else {
			state.put("isLogin", "yes");
			System.out.println(data);
			Lesson lesson = (Lesson) JSONObject.toBean(JSONObject.fromObject(data), Lesson.class);
			lesson.setTeacher(teacher);
			if (serviceFac.getLessonService().updateLesson(lesson)) {
				state.put("updateLesson", "success");
				state.put("page", "../lessonInfo");
			} else {
				state.put("updateLesson", "fail");
			}
		}

		return JSONObject.fromObject(state).toString();
	}
}
